package com.jeethink.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeethink.common.annotation.DataScope;
import com.jeethink.common.core.text.Convert;
import com.jeethink.common.exception.BusinessException;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.crm.domain.CrmOrder;
import com.jeethink.crm.domain.CrmOrderProduct;
import com.jeethink.crm.domain.WmsStockOut;
import com.jeethink.crm.domain.WmsStockProduct;
import com.jeethink.crm.mapper.CrmOrderMapper;
import com.jeethink.crm.mapper.CrmOrderProductMapper;
import com.jeethink.crm.mapper.WmsStockOutMapper;
import com.jeethink.crm.mapper.WmsStockProductMapper;
import com.jeethink.crm.service.ICrmOrderService;

/**
 * 客户订单（报价单）Service业务层处理
 * 
 * @author jeethink
 * @date 2020-03-12
 */
@Service
public class CrmOrderServiceImpl implements ICrmOrderService 
{
    @Autowired
    private CrmOrderMapper crmOrderMapper;
    
    @Autowired
    private CrmOrderProductMapper crmOrderProductMapper;
    
    @Autowired
    private WmsStockOutMapper wmsStockOutMapper;
    
    @Autowired
    private WmsStockProductMapper wmsStockProductMapper;
    

    /**
     * 查询客户订单（报价单）
     * 
     * @param orderId 客户订单（报价单）ID
     * @return 客户订单（报价单）
     */
    @Override
    public CrmOrder selectCrmOrderById(Long orderId)
    {
        return crmOrderMapper.selectCrmOrderById(orderId);
    }

    /**
     * 查询客户订单（报价单）列表
     * 
     * @param crmOrder 客户订单（报价单）
     * @return 客户订单（报价单）
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<CrmOrder> selectCrmOrderList(CrmOrder crmOrder)
    {
        return crmOrderMapper.selectCrmOrderList(crmOrder);
    }
    
    /**
     * 查询客户订单（报价单）列表    （出库选择订单产品）
     * 
     * @param crmOrder 客户订单（报价单）
     * @return 客户订单（报价单）集合
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<CrmOrder> selectOrderProductOut(CrmOrder crmOrder){
        return crmOrderMapper.selectOrderProductOut(crmOrder);    	
    }

    /**
     * 新增客户订单（报价单）
     * 
     * @param crmOrder 客户订单（报价单）
     * @return 结果
     */
    @Override
    public int insertCrmOrder(CrmOrder crmOrder)
    {
    	crmOrder.setDelFlag("0");
        crmOrder.setCreateTime(DateUtils.getNowDate());
        return crmOrderMapper.insertCrmOrder(crmOrder);
    }

    /**
     * 修改客户订单（报价单）
     * 
     * @param crmOrder 客户订单（报价单）
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCrmOrder(CrmOrder crmOrder)
    {
        crmOrder.setUpdateTime(DateUtils.getNowDate());
        crmOrderProductMapper.updateOrderProductStatus(crmOrder.getOrderId());
        return crmOrderMapper.updateCrmOrder(crmOrder);
    }

    /**
     * 删除客户订单（报价单）对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCrmOrderByIds(String ids)
    {
    	Long[] orderIds = Convert.toLongArray(ids);
    	for(int j=0;j<orderIds.length;j++){    
    		Long orderId=orderIds[j];
    		CrmOrderProduct crmOrderProduct=new CrmOrderProduct();
        	crmOrderProduct.setOrderId(orderId);
        	crmOrderProduct.setDelFlag("0");
        	List<CrmOrderProduct> list = crmOrderProductMapper.selectCrmOrderProductList(crmOrderProduct);
        	if(list.size()>0)
        	{
        		throw new BusinessException(String.format("订单号%1$s存在关联的产品,不能删除", orderId));
        	}
    	}
    	//crmOrderProductMapper.deleteCrmOrderProductByOrderIds(Convert.toStrArray(ids));
        return crmOrderMapper.deleteCrmOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除客户订单（报价单）信息
     * 
     * @param orderId 客户订单（报价单）ID
     * @return 结果
     */
    @Override
    public int deleteCrmOrderById(Long orderId)
    {
        return crmOrderMapper.deleteCrmOrderById(orderId);
    }
    
    /**
     * 订单-出库
     * 
     * @param crmOrder 客户订单
     * @return 结果
     */
    @Override
    @Transactional
    public int outCrmOrder(Long orderId,String operName) {
    	Date date=DateUtils.getNowDate();
    	WmsStockOut wmsStockOut=new WmsStockOut();
		CrmOrderProduct crmOrderProduct=new CrmOrderProduct();
    	crmOrderProduct.setOrderId(orderId);
    	crmOrderProduct.setDelFlag("0");
    	List<CrmOrderProduct> list = crmOrderProductMapper.selectCrmOrderProductList(crmOrderProduct);
    	//2、插入到出库产品表
    	for(CrmOrderProduct vo:list) {
    		if(vo.getProductOutCount()>0 && vo.getProductOutCount()<=vo.getProductCount()) {
    	    	//1、增加出库单
    	    	wmsStockOut.setTitle("订单出库");
    	    	wmsStockOut.setOrderId(orderId);
    	    	wmsStockOut.setStorageId(1L);//后续处理
    	    	wmsStockOut.setOutStatus("0");//已保存
    	    	wmsStockOut.setOutType("1");
    	    	wmsStockOut.setCreateBy(operName);
    	    	wmsStockOut.setOutBy(operName);
    	    	wmsStockOut.setDelFlag("0");
    	        wmsStockOut.setOutDate(date);
    	        wmsStockOut.setCreateTime(date);
    	        wmsStockOutMapper.insertWmsStockOut(wmsStockOut);
    		}
    		if(vo.getProductOutCount()==0) {
    			continue;
    		}
    		WmsStockProduct product=new WmsStockProduct();
    		product.setOpId(Convert.toLong(wmsStockOut.getOutId()));
    		product.setProductId(vo.getProductId());
    		product.setProductName(vo.getProductName());
    		product.setProductNum1(vo.getProductOutCount());
    		product.setType("1");// 出库
    		product.setDelFlag("0");
    		product.setCreateBy(operName);
    		product.setUpdateTime(date);
    		wmsStockProductMapper.insertWmsStockProduct(product);
    	}
    	if(wmsStockOut.getOutId()==null) {
            throw new BusinessException("出库数量不能为0");
    	}    	
    	return 1;
    }
}
